package com.p595263720.o_amap.map;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.google.gson.Gson;
import com.p595263720.o_amap.LifecycleProvider;
import com.p595263720.o_amap.OAmapPlugin;
import com.p595263720.o_amap.map.models.UnifiedMarkerOptions;

import java.util.concurrent.atomic.AtomicInteger;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

public class MyMapView implements PlatformView, EventChannel.StreamHandler, OnMarkerClickListener, DefaultLifecycleObserver, ActivityPluginBinding.OnSaveInstanceStateListener, AMap.OnMapClickListener {

    private com.amap.api.maps.MapView mMapView;
    private Context mContext;
    private boolean disposed = false;
    private int mViewId;
    private EventChannel.EventSink mEvents;

    public MyMapView(Context context, AMapOptions aMapOptions, int ViewId, BinaryMessenger binaryMessenger,
                     LifecycleProvider lifecycleProvider) {
        this.mContext = context;
        this.mViewId = ViewId;
        mMapView = new com.amap.api.maps.MapView(mContext, aMapOptions);
        lifecycleProvider.getLifecycle().addObserver(this);
        setup(binaryMessenger);
    }

    public void setup(BinaryMessenger binaryMessenger) {

        new MethodChannel(binaryMessenger, "com.p595263720.com/map" + mViewId).setMethodCallHandler(new MapHandlers(mContext, mMapView.getMap()));
        new EventChannel(binaryMessenger, "com.p595263720.com/marker_clicked" + mViewId).setStreamHandler(this);
        this.mMapView.getMap().setOnMarkerClickListener(this);
        this.mMapView.getMap().addOnMapClickListener(this);
    }

    @Override
    public View getView() {
        return mMapView;
    }

    @Override
    public void dispose() {
        if (disposed) {
            return;
        }
        disposed = true;
        destroyMapViewIfNecessary();
        mMapView.onDestroy();
    }

    @Override
    public void onListen(Object arguments, EventChannel.EventSink events) {
        mEvents = events;
    }

    @Override
    public void onCancel(Object arguments) {

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        if (mEvents != null) {
            try {
                mEvents.success(new Gson().toJson(new UnifiedMarkerOptions(marker.getOptions(), marker)));

            } catch (Exception e) {

            }
        }
        return true;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle bundle) {
        try {
            if (disposed) {
                return;
            }
            mMapView.onSaveInstanceState(bundle);
        } catch (Throwable e) {

        }
    }

    @Override
    public void onRestoreInstanceState(@Nullable Bundle bundle) {
        try {
            if (disposed) {
                return;
            }
            mMapView.onCreate(bundle);
        } catch (Throwable e) {

        }
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        try {
            if (disposed) {
                return;
            }
            if (null != mMapView) {
                mMapView.onCreate(null);
            }
        } catch (Throwable e) {
        }
    }

    @Override
    public void onStart(@NonNull LifecycleOwner owner) {

    }

    @Override
    public void onResume(@NonNull LifecycleOwner owner) {
        try {
            if (disposed) {
                return;
            }
            if (null != mMapView) {
                mMapView.onResume();
            }
        } catch (Throwable e) {

        }
    }

    @Override
    public void onPause(@NonNull LifecycleOwner owner) {
        try {
            if (disposed) {
                return;
            }
            mMapView.onPause();
        } catch (Throwable e) {
        }
    }

    @Override
    public void onStop(@NonNull LifecycleOwner owner) {

    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        try {
            if (disposed) {
                return;
            }
            destroyMapViewIfNecessary();
        } catch (Throwable e) {

        }
    }

    private void destroyMapViewIfNecessary() {
        if (mMapView == null) {
            return;
        }
        mMapView.onDestroy();
    }

    @Override
    public void onMapClick(LatLng latLng) {
        mEvents.success(JSON.toJSONString(latLng));
    }
}